package com.example.choupaiapp.common.finalokgo.request;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.choupaiapp.common.finalokgo.TypeTake;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.common.util.ContextUtil;
import com.example.choupaiapp.common.util.GsonUtil;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.ToastUtil;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.ChoseLoginActivity;
import com.example.choupaiapp.main.constant.Url;
import com.hyphenate.chat.EMClient;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ASUS on 2018/11/1.
 */

public class FinalOkgo {

    protected Context context;


    public FinalOkgo(Context context) {
        this.context = context;
    }

    /**
     * @param method
     * @param url
     * @param params
     * @param tag
     * @param callback
     * @description 普通请求
     */
    public void request(@RequestType.RequestTypes String method, String url, @Nullable HttpParams params, String tag, Callback callback) {
        if (params == null) {
            params = new HttpParams();
        }
        switch (method) {
            case RequestType.GET:
                get(url, tag, callback);
                break;
            case RequestType.GETLIST:
                getList(url, tag, callback);
                break;
            case RequestType.POST:
                post(url, params, tag, callback);
                break;
            case RequestType.POSTLIST:
                postList(url, params, tag, callback);
                break;
            case RequestType.POSTFILE:
                postFile(url, params, tag, callback);
                break;
            case RequestType.POSTJSON:
                postJson(url, params, tag, callback);
                break;
        }
    }

    public void requestHead(@RequestType.RequestTypes String method, String url, @Nullable HttpParams params, String tag, Callback callback) {
        if (params == null) {
            params = new HttpParams();
        }
        switch (method) {
            case RequestType.GETHEAD:
                getHead(url, tag, callback);
                break;
            case RequestType.GETLISTHEAD:
                getListHead(url, tag, callback);
                break;
            case RequestType.POSTHEAD:
                postHead(url, params, tag, callback);
                break;
            case RequestType.POSTLISTHEAD:
                postListHead(url, params, tag, callback);
                break;
            case RequestType.POSTFILEHEAD:
                postFileHead(url, params, tag, callback);
                break;
            case RequestType.POSTJSONHEAD:
                postJsonHead(url, params, tag, callback);
                break;
            case RequestType.POSTJSONHEADNOCACHE:
                postJsonHeadNoCache(url, params, tag, callback);
                break;
            case RequestType.POSTJSONFILEHEAD:
                postJsonFileHead(url, params, tag, callback);
                break;
        }
    }

    private void postJson(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .upJson(tag)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postJsonHeadNoCache(String url, HttpParams params, String tag, final Callback callback) {
        if (url.equals(Url.COMMENT_PRAISE)) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "praiseID");
        } else if (url.equals(Url.SHARE_SUCCESS)) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "shareID");
        }
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .upJson(tag)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }


    private void postJsonHead(String url, HttpParams params, String tag, final Callback callback) {
        if (url.equals(Url.COMMENT_PRAISE)) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "praiseID");
        } else if (url.equals(Url.SHARE_SUCCESS)) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "shareID");
        }
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .upJson(tag)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }


    private void get(String url, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>get(url)
                .tag(tag)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }


    private void getHead(String url, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>get(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void login(Response<String> response) {
        try {
            JSONObject jsonObject = new JSONObject(response.body());
            int code = jsonObject.getInt("code");
            if (code == 401) {
                EventBus.getDefault().post("main_index_1");
                EMClient.getInstance().logout(true);
                SharedPreferencesHelp.remove("token");
                SharedPreferencesHelp.remove("userInfo");
                JPushInterface.deleteAlias(context, 0);
                //登出
                MobclickAgent.onProfileSignOff();
//                ActivityManager.getActivityManager().popAllActivity();
                startActivity(ChoseLoginActivity.class);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getList(String url, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>get(url)
                .tag(tag)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void getListHead(String url, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>get(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void post(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        Logger.e(response.body() + "---" + response.message() + "---" + response.getException() + "---");
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postHead(String url, HttpParams params, String tag, final Callback callback) {
        if (url.contains(Url.ADD_BLACK_LIST)) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "shieldingID");
        }
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            e.printStackTrace();
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postFile(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .isMultipart(true)
                .tag(tag)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postJsonFileHead(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .isMultipart(true)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .upJson(tag)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postFileHead(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .isMultipart(true)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }

    private void postList(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .params(params)
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }


    private void postListHead(String url, HttpParams params, String tag, final Callback callback) {
        String time = System.currentTimeMillis() + "";
        OkGo.<String>post(url)
                .tag(tag)
                .headers("token", (String) SharedPreferencesHelp.get("token", ""))
                .headers("cp", Utils.mmd5(time + "DFTS").toLowerCase())
                .headers("lvluo", time)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Logger.e(response.body());
                        login(response);
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t((clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtil.showImageToas("网络开小差了...");
                        callback.onError(response);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        Logger.e(response.body());
                        Class clasz = TypeTake.getClass(callback);
                        try {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-success").e(response.body());
                            callback.onSuccess(parse(response.body(), clasz));
                        } catch (Exception e) {
                            Logger.t("cache" + (clasz + "").substring((clasz + "").lastIndexOf(".") + 1) + "-exception").e(e.getMessage());
                            callback.onPaseException(e);
                            callback.onError(response);
                        }
                    }
                });
    }


    private Object parse(String json, Class type) {
        Object obj = GsonUtil.jsonStringToClassWithGson(json, type);
        return obj;
    }

    private List<Object> parseList(String json, Class type) {
        List<Object> list = GsonUtil.jsonToList(json, type);
        return list;
    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
//        ActivityCompat.startActivity(context, intent, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context).toBundle());
        context.startActivity(intent);
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(context, cls);
//        ActivityCompat.startActivity(context, intent, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context).toBundle());
        context.startActivity(intent);
    }

    public void startFinishActivity(Class<?> cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    public void startFinishActivity(Class<?> cls, @Nullable Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    public void finish() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ((Activity) context).finishAfterTransition();
        } else {
            ((Activity) context).finish();
        }
    }

}
